﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;



public partial class InventoryTracker_CustomerMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCustConti.Visible = false;

        btnCustNew.Enabled = true;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = true;
        btnCustUpdate.Enabled = false;
        btnCustConti.Enabled = false;

    }
   
    protected void btnCustSubmit_Click(object sender, EventArgs e)
    {
        if (txtCustAddr2ID.Text == null || txtCustAddr2ID.Text == "")
        {
            txtCustAddr2ID.Text = "-";
        }
        if (txtCustAddr3ID.Text == null || txtCustAddr3ID.Text == "")
        {
            txtCustAddr3ID.Text = "-";
        }
        if (txtCustMobile.Text == null || txtCustMobile.Text == "")
        {
            txtCustMobile.Text = "0";
        }
        if (txtCustFax.Text == null || txtCustFax.Text == "")
        {
            txtCustFax.Text = "0";
        }
        if (txtCustEMail.Text == null || txtCustEMail.Text == "")
        {
            txtCustEMail.Text = "-";
        }



        btnCustConti.Visible = true;

        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = false;
        btnCustConti.Enabled = true;


        DataAccess DA = DataAccessHelper.GetDataAccess();
        Customer aCustomer = new Customer(txtCustName.Text, txtCustAddr1ID.Text, txtCustAddr2ID.Text, txtCustAddr3ID.Text,
            txtCustCity.Text, txtCustState.Text, txtCustCountry.Text, Int64.Parse(txtCustPhone.Text), Int64.Parse(txtCustMobile.Text), Int64.Parse(txtCustFax.Text),
            txtCustEMail.Text, Int64.Parse(txtCustZipCode.Text), int.Parse(txtCustLeadTime.Text), RDbtnListCust.SelectedValue);
            //int.Parse(txtCustStatus.Text) );
        try
        {
            if (DA.CreateCustomer(aCustomer))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Failure";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }

    }
    protected void DdlCustomerName_SelectedIndexChanged(object sender, EventArgs e)
    {
        String SelectValue = DdlCustomerName.SelectedValue;

        btnCustNew.Enabled = true;
        btnCustDelete.Enabled = true;
        btnCustEdit.Enabled = true;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM  CustomerMaster WHERE  [CustID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtCustName.Text = dataTable.Rows[0]["CustName"].ToString(); 
        txtCustAddr1ID.Text = dataTable.Rows[0]["CustAddr1"].ToString();
        txtCustAddr2ID.Text = dataTable.Rows[0]["CustAddr2"].ToString();
        txtCustAddr3ID.Text = dataTable.Rows[0]["CustAddr3"].ToString();
        txtCustCity.Text = dataTable.Rows[0]["CustCity"].ToString();
        txtCustState.Text = dataTable.Rows[0]["CustState"].ToString();
        txtCustCountry.Text = dataTable.Rows[0]["CustCountry"].ToString();
        txtCustPhone.Text =  dataTable.Rows[0]["CustPhone"].ToString();
        txtCustFax.Text = dataTable.Rows[0]["CustFax"].ToString();
        txtCustMobile.Text = dataTable.Rows[0]["CustMobile"].ToString();
        txtCustEMail.Text = dataTable.Rows[0]["CustEmail"].ToString();
        txtCustZipCode.Text = dataTable.Rows[0]["CustZipCode"].ToString();
        txtCustLeadTime.Text = dataTable.Rows[0]["CustLeadTime"].ToString();
       // txtCustStatus.Text = dataTable.Rows[0]["CustStatus"].ToString();
        RDbtnListCust.SelectedValue = dataTable.Rows[0]["CustStatus"].ToString();
        con.Close();


    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void btnCustNew_Click(object sender, EventArgs e)
    {
        
        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = true;
        btnCustUpdate.Enabled = false;

        txtCustName.Text = "";
        txtCustAddr1ID.Text = "";
        txtCustAddr2ID.Text = "";
        txtCustAddr3ID.Text = "";
        txtCustCity.Text = "";
        txtCustState.Text = "";
        txtCustCountry.Text = "";
        txtCustPhone.Text = "";
        txtCustFax.Text = "";
        txtCustMobile.Text = "";
        txtCustEMail.Text = "";
        txtCustZipCode.Text = "";
        txtCustLeadTime.Text = "";
        //txtCustStatus.Text = "";
        RDbtnListCust.SelectedValue = "";


    }
    protected void btnCustDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlCustomerName.SelectedValue;

        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = true;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM CustomerMaster WHERE  [CustID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtCustName.Text = "";
        txtCustAddr1ID.Text = "";
        txtCustAddr2ID.Text = "";
        txtCustAddr3ID.Text = "";
        txtCustCity.Text = "";
        txtCustState.Text = "";
        txtCustCountry.Text = "";
        txtCustPhone.Text = "";
        txtCustFax.Text = "";
        txtCustMobile.Text = "";
        txtCustEMail.Text = "";
        txtCustZipCode.Text = "";
        txtCustLeadTime.Text = "";
        //txtCustStatus.Text = ""; 
        RDbtnListCust.SelectedValue = "";

    }
    protected void btnCustEdit_Click(object sender, EventArgs e)
    {
        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = true;

       
    }
    protected void btnCustUpdate_Click(object sender, EventArgs e)
    {
        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = true;

        String SelectValue = DdlCustomerName.SelectedValue;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE CustomerMaster SET  CustName = '" + txtCustName.Text + "' ,CustAddr1 = '" + txtCustAddr1ID.Text + "' , CustAddr2 = '" + txtCustAddr2ID.Text + "', CustAddr3 = '" + txtCustAddr3ID.Text + "' ,CustCity= '" + txtCustCity.Text + "',CustState = '" + txtCustState.Text + "', CustCountry= '" + txtCustCountry.Text + "',CustPhone= '" + txtCustPhone.Text + "', CustMobile=  '" + txtCustMobile.Text + "',CustFax = '" + txtCustFax.Text + "', CustEmail = '" + txtCustEMail.Text + "',CustZipCode= '" + txtCustZipCode.Text + "',CustLeadTime= '" + txtCustLeadTime.Text + "',CustStatus= '" + RDbtnListCust.SelectedValue + "'  WHERE [CustID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtCustName.Text = "";
        txtCustAddr1ID.Text = "";
        txtCustAddr2ID.Text = "";
        txtCustAddr3ID.Text = "";
        txtCustCity.Text = "";
        txtCustState.Text = "";
        txtCustCountry.Text = "";
        txtCustPhone.Text = "";
        txtCustFax.Text = "";
        txtCustMobile.Text = "";
        txtCustEMail.Text = "";
        txtCustZipCode.Text = "";
        txtCustLeadTime.Text = "";
        //txtCustStatus.Text = "";
        RDbtnListCust.SelectedValue = "";
    }
    protected void btnCustConti_Click(object sender, EventArgs e)
    {
        btnCustConti.Visible = true;

        btnCustNew.Enabled = false;
        btnCustDelete.Enabled = false;
        btnCustEdit.Enabled = false;
        btnCustSubmit.Enabled = false;
        btnCustUpdate.Enabled = false;
        btnCustConti.Enabled = true;

        Response.Redirect ("View.aspx");

    }
}
